package cn.tristenstudy.blog.controller;

import cn.tristenstudy.blog.pojo.Blog;
import cn.tristenstudy.blog.pojo.dto.TagDTO;
import cn.tristenstudy.blog.pojo.vo.BlogQuery;
import cn.tristenstudy.blog.service.BlogService;
import cn.tristenstudy.blog.service.TagService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 标签展示
 *
 * @author xichen1.li
 * @date 2021-01-13 10:51
 */
@Controller
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/{id}/{current}")
    public String tags(@PathVariable("id") Long id, @PathVariable("current") Integer current, Model model) {
        List<TagDTO> tagList = tagService.listTagTop(10000);
        if (id == -1) {
            id = tagList.get(0).getId();
        }
        Page<Blog> blogPage = tagService.listBlogByTagId(id, current);
        model.addAttribute("tags", tagList);
        model.addAttribute("page", blogPage);
        model.addAttribute("activeTagId", id);
        model.addAttribute("first", blogPage.hasPrevious());
        model.addAttribute("last", blogPage.hasNext());
        return "tags";
    }

}
